package model.dao;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;

public class LogIO {
	
	private Path path = Paths.get("files/log.txt");
	private Charset utf8 = StandardCharsets.UTF_8;
	
	public void gravarLogin(String nome) throws IOException{
	
		ArrayList<String> recebeLinhas= new ArrayList<>();
		recebeLinhas=recuperaLinhas();
		
		try(BufferedWriter writer = Files.newBufferedWriter(path,utf8)){
					
			for(String l : recebeLinhas){
				writer.write(l+"\n");
			}
			
			writer.write("Usuario "+nome+" logou: "+new Date()+" ;");
		}
	}
	
	public void gravarLogoff() throws IOException{
		
		
		try(BufferedWriter writer = Files.newBufferedWriter(path,utf8)){
			
			writer.write("Deslogou: "+new Date()+"/n");
		}
	}
	
	public ArrayList<String> recuperaLinhas() throws IOException{
		
		ArrayList<String> linhas = new ArrayList<>();
		
		try(BufferedReader rder = Files.newBufferedReader(path,utf8)){
			String line=null;
			
			while ((line=rder.readLine())!=null){				
				linhas.add(line);
			}
		}
		
		return linhas;
		
	}

}
